package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查组实现
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     *  添加检查组
     * @param checkitemIds
     * @param checkGroup
     */
    @Override
    @Transactional //事务控制
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //添加检查组
        checkGroupMapper.add(checkGroup);
        if(checkitemIds != null && checkitemIds.length > 0){
            //添加检查组和检查项的关联关系
            for (Integer checkitemId : checkitemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
            }
        }
    }
    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.执行查询
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryPageBean.getQueryString());
        //3.封装到PageResult中并返回
        return new PageResult(page.getTotal(), page.getResult());
    }
    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    /**
     * 检查组实现
     * @param checkitemIds
     * @param checkGroup
     */
    @Override
    @Transactional
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        //1.编辑检查组
        checkGroupMapper.edit(checkGroup);
        //2.删除检查组和检查项的关联关系
        checkGroupMapper.delet(checkGroup.getId());
        if(checkitemIds != null && checkitemIds.length > 0){
            //3.重新建立检查组和检查项的关联关系
            for (Integer checkitemId : checkitemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
            }
        }
    }

    @Override
    public Boolean delete(Integer id) {
        //1. 根据检查组id，查询是否有关联套餐项
        long count = checkGroupMapper.findCountByCheckItemId(id);
        if (count > 0) {
            throw new RuntimeException("当前检查组被套餐引用，不能删除");
        }
        //2. 删除检查组和检查项的关联关系
        checkGroupMapper.delet(id);
        int i = checkGroupMapper.delete(id); //3.删除检查组
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    @Override
    public Integer[] findCheckGroupIdsBySetMealId(Integer id) {
        return checkGroupMapper.findCheckGroupIdsBySetMealId(id);
    }

}
